//
//  PostViewController.m
//  xxShare
//
//  Created by 柳麟喆 on 2018/3/5.
//  Copyright © 2018年 lzLiu. All rights reserved.
//

#import "PostViewController.h"
#import "CameraViewController.h"
#import "TZImagePickerController.h"
#import "PublishViewController.h"

#define SCREEN_WIDTH [UIScreen mainScreen].bounds.size.width
#define SCREEN_HEIGHT [UIScreen mainScreen].bounds.size.height

@interface PostViewController ()<TZImagePickerControllerDelegate>

///关闭
@property(nonatomic, strong)UIButton *closeBtn;
///照相
@property(nonatomic, strong)UIButton *takePhotoBtn;
///图库
@property(nonatomic, strong)UIButton *albumBtn;
@property(nonatomic, strong)NSMutableArray <UIButton *> *btnArrays;

@end

@implementation PostViewController

- (UIButton *)closeBtn{
    if (!_closeBtn) {
        _closeBtn = [[UIButton alloc]initWithFrame:CGRectMake(0,0, 40, 40)];
        _closeBtn.center = CGPointMake(SCREEN_WIDTH/2, SCREEN_HEIGHT-25);
        [_closeBtn setBackgroundImage:[UIImage imageNamed:@"关闭-黄色-48.png"] forState:UIControlStateNormal];
        [_closeBtn addTarget:self action:@selector(closeVC) forControlEvents:UIControlEventTouchUpInside];
    }
    return _closeBtn;
}

- (UIButton *)takePhotoBtn{
    if (!_takePhotoBtn) {
        _takePhotoBtn = [[UIButton alloc]initWithFrame:CGRectMake(0,0, 64, 64)];
        _takePhotoBtn.center = CGPointMake(SCREEN_WIDTH/4, SCREEN_HEIGHT-150);
        [_takePhotoBtn setBackgroundImage:[UIImage imageNamed:@"相机64.png"] forState:UIControlStateNormal];
        [_takePhotoBtn addTarget:self action:@selector(takePhotoBtnClick) forControlEvents:UIControlEventTouchUpInside];
        _takePhotoBtn.transform = CGAffineTransformMakeTranslation(0, 220);
    }
    return _takePhotoBtn;
}

- (UIButton *)albumBtn{
    if (!_albumBtn) {
        _albumBtn = [[UIButton alloc]initWithFrame:CGRectMake(0,0, 64, 64)];
        _albumBtn.center = CGPointMake(SCREEN_WIDTH/4*3, SCREEN_HEIGHT-150);
        [_albumBtn setBackgroundImage:[UIImage imageNamed:@"图片64.png"] forState:UIControlStateNormal];
        [_albumBtn addTarget:self action:@selector(albumBtnClick) forControlEvents:UIControlEventTouchUpInside];
        _albumBtn.transform = CGAffineTransformMakeTranslation(0, 220);
    }
    return _albumBtn;
}

///初始化
- (void)setUp{
    self.view.backgroundColor = [UIColor clearColor];
    self.view.layer.contents = (__bridge id _Nullable)([UIImage imageNamed:@"shareBottomBackground"].CGImage);
    [self.view addSubview:self.closeBtn];
    [self.view addSubview:self.takePhotoBtn];
    [self.view addSubview:self.albumBtn];
    self.btnArrays = [[NSMutableArray alloc]init];
    [self.btnArrays addObject:self.takePhotoBtn];
    [self.btnArrays addObject:self.albumBtn];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setUp];
}

- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:YES];
    [self.btnArrays enumerateObjectsUsingBlock:^(UIButton * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        [UIView animateWithDuration:0.5 delay:idx * 0.1 usingSpringWithDamping:0.4 initialSpringVelocity:0.4 options:UIViewAnimationOptionCurveEaseIn animations:^{
            
            obj.transform = CGAffineTransformIdentity;
            
        } completion:nil];
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

///关闭此页面
- (void)closeVC{
    [self dismissViewControllerAnimated:YES completion:nil];
}

///照相
- (void)takePhotoBtnClick{
    CameraViewController *camera = [[CameraViewController alloc]init];
    [self presentViewController:camera animated:YES completion:nil];
}

///从图库选择
- (void)albumBtnClick{
    TZImagePickerController *picker = [[TZImagePickerController alloc]initWithMaxImagesCount:9 delegate:self];
    picker.allowTakePicture = NO;
    [picker setDidFinishPickingPhotosHandle:^(NSArray<UIImage *> *photos, NSArray *assets, BOOL isSelectOriginalPhoto) {
        
        PublishViewController *publish = [[PublishViewController alloc]init];
        publish.selectedAssets = [NSMutableArray arrayWithArray:assets];
        publish.selectedPhotos = [NSMutableArray arrayWithArray:photos];
        publish.isSelectOriginalPhoto = isSelectOriginalPhoto;
        UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:publish];
        [self presentViewController:nav animated:YES completion:nil];
    }];
    [self presentViewController:picker animated:YES completion:nil];
}
@end
